﻿using UnityEngine;

/// <summary>
/// Tween the object's position by an amount (or move by)
/// </summary>

[AddComponentMenu("NGUI/Tween/Tween Position By Amount")]
public class TweenPositionBy : UITweener
{
    public Vector3 from;
    //public Vector3 to;

    public Vector3 amount;

    Transform mTrans;

    public Transform cachedTransform { 
        get {
            if (mTrans == null)
            {
                mTrans = transform;
                from = mTrans.localPosition;
            }
            return mTrans; 
        } 
    }
    public Vector3 position { get { return cachedTransform.localPosition; } set { cachedTransform.localPosition = value; } }

    protected override void OnUpdate(float factor, bool isFinished) { 
        cachedTransform.localPosition = from * (1f - factor) + (from + amount) * factor; 
    }

    /// <summary>
    /// Start the tweening operation.
    /// </summary>

    static public TweenPositionBy Begin(GameObject go, float duration, Vector3 amount)
    {
        TweenPositionBy comp = UITweener.Begin<TweenPositionBy>(go, duration);
        comp.from = comp.position;
        comp.amount = amount;

        if (duration <= 0f)
        {
            comp.Sample(1f, true);
            comp.enabled = false;
        }
        return comp;
    }
}
